package com.tjvib.view.activity;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.view.ViewGroup;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.just.agentweb.AgentWeb;
import com.just.agentweb.DefaultWebClient;
import com.just.agentweb.WebChromeClient;
import com.tjvib.R;
import com.tjvib.base.BaseActivity;
import com.tjvib.presenter.BasePresenter;
import com.tjvib.util.LogUtil;
import com.tjvib.util.Manager.AppManager;
import com.tjvib.widget.TitleLayout;

public class WebActivity extends BaseActivity {

    private TitleLayout web_tit;
    private LinearLayout web_ll;

    private AgentWeb mAgentWeb;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int initLayout() {
        return R.layout.activity_web;
    }

    @Override
    protected boolean initView() {
        web_tit = findViewById(R.id.web_tit);
        web_ll = findViewById(R.id.web_ll);

        return true;
    }

    @SuppressLint("SetJavaScriptEnabled")
    @Override
    protected boolean initData() {
        String url = getIntent().getStringExtra("URL");
        String title = getIntent().getStringExtra("TITLE");
        web_tit.setTitle(title);

//        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP)
//            web_wv.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
//        web_wv.clearCache(true);
//        web_wv.clearHistory();
//        web_wv.requestFocus();
//        web_wv.getSettings().setDatabaseEnabled(true);
//        web_wv.getSettings().setDomStorageEnabled(true);
//        web_wv.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
//        web_wv.setWebChromeClient(new WebChromeClient());
//        web_wv.setWebViewClient(new WebViewClient() {
//            @Override
//            public void onPageStarted(WebView view, String url, Bitmap favicon) {
//                super.onPageStarted(view, url, favicon);
//                LogUtil.e("start"+url);
//            }
//
//            @Override
//            public void onPageFinished(WebView view, String url) {
//                super.onPageFinished(view, url);
//                LogUtil.e("finish"+url);
//            }
//
//            @Override
//            public boolean shouldOverrideUrlLoading(WebView view, String url) {
//                // 重写此方法表明点击网页里面的链接还是在当前的webview里跳转，不另跳浏览器
//                // 在2.3上面不加这句话，可以加载出页面，在4.0上面必须要加入，不然出现白屏
//                LogUtil.e("should"+url);
//                if (url.startsWith("http://") || url.startsWith("https://")) {
//                    view.loadUrl(url);
//                    web_wv.stopLoading();
//                    return true;
//                }
//                return false;
//            }
//
//            @Override
//            public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
//                // 处理错误
//                LogUtil.e("error"+error);
//            }
//        });
//        web_wv.loadUrl(getIntent().getStringExtra("URL"));

        mAgentWeb = AgentWeb.with(this)
                .setAgentWebParent(web_ll, new LinearLayout.LayoutParams(-1, -1))
                .useDefaultIndicator(R.color.main_purple_0)
                .setMainFrameErrorView(R.layout.activity_not_found, -1)
                .setSecurityType(AgentWeb.SecurityType.STRICT_CHECK)
                .setOpenOtherPageWays(DefaultWebClient.OpenOtherPageWays.ASK)
                .interceptUnkownUrl() //拦截找不到相关页面的Scheme
                .createAgentWeb()
                .ready()
                .go(url);

        mAgentWeb.getAgentWebSettings().getWebSettings().setAllowFileAccess(true);
        mAgentWeb.clearWebCache();

        return true;
    }

    @Override
    protected BasePresenter genPresenter() {
        return null;
    }

    @Override
    protected void onResume() {
        mAgentWeb.getWebLifeCycle().onResume();
        super.onResume();
    }

    @Override
    protected void onPause() {
        mAgentWeb.getWebLifeCycle().onPause();
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mAgentWeb.getWebLifeCycle().onDestroy();
    }
}